<?php
class Merchant_Model_QualityControlOrder
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_QualityControlOrder();
	}
	
	public static function getInstance(){
		return new Merchant_Model_QualityControlOrder();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet $row quality_control_order data 
	*@return boolean
	*/
	public  function add($row){
		$row['qco_create_time'] = date("Y-m-d H:i:s");
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row quality_control_order data 
	*@param int $qco_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$row['qco_last_update'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto('qco_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
	
	public function updateByQcoCode($row,$qco_code){
		$where = $this->_table->getAdapter()->quoteInto('qco_code = ?', $qco_code);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $qco_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('qco_id = ?', $id); 
		return $this->_table->delete($where);
	}
	
	 public function deleteByQcoCode($qco_code){
		$where = $this->_table->getAdapter()->quoteInto('qco_code = ?', $qco_code); 
		return $this->_table->delete($where);
	}
	
	/**
	* @param int $qco_code
	* @return zend_db_table_row
	*/
	public function getQcoCode($qco_code){
		$where = $this->_table->getAdapter()->quoteInto("qco_code = ?",$qco_code);
		return $this->_table->fetchRow($where);
	}
	
	/**
	* @param int $qco_ref_code
	* @return zend_db_table_row
	*/
	public function getByQcoRefCode($qco_ref_code){
		$where = $this->_table->getAdapter()->quoteInto("qco_ref_code = ?",$qco_ref_code);
		return $this->_table->fetchAll($where);
	}
	
	public function getByCond($cond,$type,$order='',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		$select->where('customer_id = ?',$cond['customer_id']);
		if(isset($cond['qco_status']) && $cond['qco_status'] !==''){
			$select->where('qco_status = ?',$cond['qco_status']);
		}
		if(isset($cond['qco_code']) && $cond['qco_code'] !=''){
			$select->where('qco_code = ?',$cond['qco_code']);
		}
		if(isset($cond['qco_ref_code']) && $cond['qco_ref_code'] !=''){
			$select->where('qco_ref_code = ?',$cond['qco_ref_code']);
		}
		if(isset($cond['dateFrom']) && $cond['dateFrom'] !=''){
			$select->where('qco_create_time > ?',$cond['dateFrom'].' 00:00:00');
		}
		if(isset($cond['dateTo']) && $cond['dateTo'] !=''){
			$select->where('qco_create_time < ?',$cond['dateTo'].' 23:59:59');
		}
		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        if ($count==0){        	
        	return $this->_table->fetchAll($select)->toArray(); 
        }
		$select->order($order);
		$select->limit($count,$offset);
		
		return $this->_table->fetchAll($select)->toArray();
	}
	
    public function getByCondition($condition=array(),$type='*',$orderby="qco_id",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["qco_code"]) && $condition["qco_code"]!=''){
             $select->where(" qco_code = ?",$condition["qco_code"]);
        }
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
            $select->where(" customer_id = ?",$condition["customer_id"]);
        }
		if(isset($condition["qco_status"]) && $condition["qco_status"]!=''){
            $select->where(" qco_status = ?",$condition["qco_status"]);
        }
		if(isset($condition["user_id"]) && $condition["user_id"]!=''){
            $select->where(" user_id = ?",$condition["user_id"]);
        }
		if(isset($condition["qco_ref_code"]) && $condition["qco_ref_code"]!=''){
            $select->where(" qco_ref_code = ?",$condition["qco_ref_code"]);
        }
		if(isset($condition["date_from"]) && $condition['date_from'] != ""){
			$select->where(" qco_create_time>=?",$condition['date_from']);
		}
		if(isset($condition["date_to"]) && $condition['date_to'] != ""){
			$select->where(" qco_create_time<=?",$condition['date_to']);
		}
		
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}	
	
	/**
	 * 获取当天最后一个 qco_code
	 */
	public function getCurdateLastCode(){
		$select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'max(qco_code)');
        $mindate = date('Y-m-d').' 00:00:00';
        $maxdate = date('Y-m-d').' 23:59:59';
        $select->where("qco_create_time > ?",$mindate);
        $select->where("qco_create_time < ?",$maxdate);
		return $this->_table->getAdapter()->fetchOne($select);
	}
	
}